package com.example.administrator.newsapp;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Method;

import cn.bmob.v3.BmobUser;

public class MainActivity extends AppCompatActivity {
    public static final int LOGIN_SUC = 1001;
    private DrawerLayout mDrawerLayout;
    private FragmentTabHost mFragmentTabHost;
    private String[] tabs = {"头条","体育","娱乐","社会"};
    private String[] tags = {"top", "tiyu","yule", "shehui"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar!= null){
            actionBar.setDisplayHomeAsUpEnabled(true);//让导航按钮显示出来
            actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
        }
        mDrawerLayout = findViewById(R.id.drawer_layout);

        mFragmentTabHost=findViewById(android.R.id.tabhost);
        mFragmentTabHost.setup(MainActivity.this,getSupportFragmentManager(),android.R.id.tabcontent);
//        mFragmentTabHost.getTabWidget().setDividerDrawable(null);

        for (int i = 0; i <tabs.length ; i++) {
            TabHost.TabSpec tabSpec = mFragmentTabHost.newTabSpec(tags[i]);
            tabSpec.setIndicator(getItemView(i));
            mFragmentTabHost.addTab(tabSpec,FragmentTab.class,null);

        }

//        导航栏
        NavigationView navView = findViewById(R.id.nav_view);
        navView.setCheckedItem(R.id.nav_username);
        navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.nav_username:
                        if(BmobUser.getCurrentUser()!=null){
                            Toast.makeText(MainActivity.this,"你好,当前用户是:"+BmobUser.getCurrentUser().getUsername(),Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(MainActivity.this,"你好,请先登录",Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case R.id.nav_outlogin:
                        if(BmobUser.getCurrentUser()!=null){
                            BmobUser.logOut();   //清除缓存用户对象
                            Toast.makeText(MainActivity.this,"已退出登录",Toast.LENGTH_SHORT).show();
                            invalidateOptionsMenu();
                        }else {
                            Toast.makeText(MainActivity.this,"请先登录",Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case R.id.nav_favorite:
                        if(BmobUser.getCurrentUser()!=null){
                            Intent intent =  new Intent(MainActivity.this,ShowFavoriteActivity.class);
                            startActivity(intent);
                        }else {
                            Toast.makeText(MainActivity.this,"请先登录",Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case R.id.nav_happy:
                        Intent intent = new Intent(MainActivity.this,showHappyActivity.class);
                        startActivity(intent);
                        break;

                }

                return true;
            }
        });

        //        悬浮按钮
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(MainActivity.this,"没有惊喜",Toast.LENGTH_SHORT).show();
//                可交互提示
                Snackbar.make(v,"删除数据",Snackbar.LENGTH_SHORT)
                        .setAction("返回上一步", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(MainActivity.this,"数据恢复了",Toast.LENGTH_SHORT).show();
                            }
                        }).show();
            }
        });
    }

    public View getItemView(int i) {
        View view = getLayoutInflater().inflate(R.layout.tabs,null);
        TextView textView = view.findViewById(R.id.tv_tabs);
        textView.setText(tabs[i]);
        return view;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.index_toolbar,menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem menuItem = menu.findItem(R.id.login);
        if(BmobUser.getCurrentUser()!=null) {
            menuItem.setIcon(R.drawable.ic_login);
        }else {
            menuItem.setIcon(R.drawable.ic_line);
        }
        //解决ICON不显示
        if (menu != null) {
            if(menu.getClass().getSimpleName().equals("MenuBuilder")) {
                try {
                    Method m = menu.getClass().getDeclaredMethod( "setOptionalIconsVisible", Boolean.TYPE);
                    m.setAccessible(true); m.invoke(menu, true);
                } catch (Exception e) {
                    Log.e(getClass().getSimpleName(), "unable to set icons for overflow menu", e);
                }
            }
        }
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);//将滑动菜单显示出来
                break;
            case R.id.login:
                if(BmobUser.getCurrentUser()==null){
                    Intent intent = new Intent(this, LoginActivity.class);
                    startActivityForResult(intent, LOGIN_SUC);
                }else {
                    Toast.makeText(this,"当前用户："+BmobUser.getCurrentUser().getUsername(),Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.place:
                break;
            case R.id.file:
                break;
            case R.id.muice:
                break;
            default:
        }
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==LOGIN_SUC){
            Toast.makeText(MainActivity.this,"你好,"+BmobUser.getCurrentUser().getUsername(),Toast.LENGTH_SHORT).show();
            invalidateOptionsMenu();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
